﻿using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevTools.Common.Components;
using DevTools.Properties;

namespace DevTools.Romanizer.Controls
{
    public partial class RecentList : XtraUserControl
    {
        private const string FileName = "RecentItems.txt";

        public RecentList()
        {
            InitializeComponent();
        }

        public void AddItem(string item)
        {
            lbcList.Items.Insert(0, item);

            if (lbcList.Items.Count > Settings.Default.Romanizer_MaxRecentItemCount)
                lbcList.Items.RemoveAt(lbcList.Items.Count - 1);
        }

        public void LoadData()
        {
            if (ClickOnceDataFileHelper.Exists(FileName) == false)
                return;

            var lines = ClickOnceDataFileHelper.ReadAllLines(FileName);
            foreach (var line in lines)
                lbcList.Items.Add(line);
        }

        public void SaveData()
        {
            var lines = new string[lbcList.Items.Count];

            int index = 0;
            foreach (string item in lbcList.Items)
                lines[index++] = item;

            ClickOnceDataFileHelper.WriteAllLines(FileName, lines);
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            string item = (string) lbcList.SelectedItem;
            var tokens = item.Split('\t');
            Clipboard.SetText(tokens[1]);
        }
    }
}